<?php

class Sistema_Form_Conteudo extends Zend_Form
{
    public $cur_id;
    public function init() {
   	    
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        $urlbase = $front->getBaseUrl();
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $this->setName('form_'.$controller);
        
        $id = $front->getRequest()->getParam('id', 0);
        $tpc_id = $front->getRequest()->getParam('tpc_id', 0);
        
        $url .= '/index/tpc_id/'.$tpc_id;
        
        
        if($id > 0){
           $query = 'SELECT SQL_CACHE * FROM CONTEUDO WHERE CON_ID = ' . $id;
           $data = $db->fetchRow($query);
        }
        
        $el = new Zend_Form_Element_Hidden('CON_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Hidden('TPC_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CON_NOM');
        $el->setLabel('Nome')
                ->setRequired(true)
                ->setAttrib('class', 'cmp_vlr_nom')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CON_DSC');
        $el->setLabel('Descrição')
                ->setRequired(true)
                ->setAttrib('class', 'cmp_vlr_nom')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        
        
        
        $el = new Zend_Form_Element_Hidden('CON_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int')
            ->setAttrib('class', 'ajx_vlr_id find_val')
            ->addValidator('NotEmpty');
        $this->addElement($el);
        
       
        $el = new Zend_Form_Element_File('CON_FILE');
        $el->setLabel('Arquivo');
	$el->addValidator('Extension', false, array('mp4','pdf','png','gif','jpeg'));
	//->addValidator('Size', false, 10240000);
	//$el->setDestination('public/upload/minibanner');
        $this->addElement($el);
        
        if($id > 0){
            Zend_Loader::loadClass('imagem','library');
            
            $arq = imagem::formatNomeArquivo($data['CON_ID'],$data['CON_DATHOR']);
 
            $img = 'public/upload/'.$controller.'/'.$arq.'.*';
            foreach (glob($img) as $src) {
                $ext = end(explode('.', $src));    
                if($ext == 'mp4'){
                    
                    $sessao = new Zend_Session_Namespace('Repositorio');
                    $sessao->caminho = '..'.$urlbase.'/public/upload/conteudo/';
                    $sessao->id = $data['CON_ID'];
                    $sessao->dathor = $data['CON_DATHOR'];
                    
                    Zend_Loader::loadClass('tools','library');
                    
                    $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>tools::frameVideo(575,360)));
                    $this->addElement($el);
                }
                else {
                    $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<a href="'.$front->getBaseUrl().'/'.$src.'" target="_blanck">Ver impresso</a>' ));
                    $this->addElement($el);
                }
            }
        }
        
        $el = new Zend_Form_Element_Text('CON_ORD',array('class'=>'numero'));
        $el->setLabel('Ordem')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('CON_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
          
        $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        
        $btn = new Zend_Form_Element_Button('Voltar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        
    }

}

